/*
 * Created by: vadikv
 * Created: Wednesday, March 05, 2008
 */

using System.Collections;
using System.Collections.Generic;
using OneDayModel.Interfaces;

namespace OneDayModel.Model
{
    public class Role : IRole
    {
        private readonly string name;
        private readonly bool public_;
        private readonly string parentRole;
        private readonly IEnumerable<IRoleAttribute> attributes;

        public Role(string name, bool public_, string parentRole, IEnumerable<IRoleAttribute> roleAttributes)
        {
            this.name = name;
            this.public_ = public_;
            this.parentRole = parentRole;
            attributes = roleAttributes;
        }

        public bool Public
        {
            get { return public_; }
        }

        public string Name
        {
            get { return name; }
        }

        public string Parent
        {
            get { return parentRole; }
        }

        public IEnumerable<IRoleAttribute> Attributes
        {
            get
            {
                return attributes;
            }
        }

        public IEnumerable<IRoleAttribute> GetRequiredAttributes()
        {
//            return (from attribute in Attributes
//                      where attribute.Required
//                      select attribute);
            return new RequiredAttributesEnumerable(attributes);
        }
    }

    internal class RequiredAttributesEnumerable : IEnumerable<IRoleAttribute>
    {
        private readonly IEnumerable<IRoleAttribute> attributes;

        public RequiredAttributesEnumerable(IEnumerable<IRoleAttribute> attributes)
        {
            this.attributes = attributes;
        }

        IEnumerator<IRoleAttribute> IEnumerable<IRoleAttribute>.GetEnumerator()
        {
            return new RequiredAttributesEnumerator(attributes.GetEnumerator());
        }

        public IEnumerator GetEnumerator()
        {
            return ((IEnumerable<IRoleAttribute>) this).GetEnumerator();
        }
    }

    internal class RequiredAttributesEnumerator : IEnumerator<IRoleAttribute>
    {
        private readonly IEnumerator<IRoleAttribute> enumerator;

        public RequiredAttributesEnumerator(IEnumerator<IRoleAttribute> enumerator)
        {
            this.enumerator = enumerator;
        }


        IRoleAttribute IEnumerator<IRoleAttribute>.Current
        {
            get { return enumerator.Current; }
        }

        public void Dispose()
        {
            enumerator.Dispose();
        }

        public bool MoveNext()
        {
            while (enumerator.MoveNext())
            {
                if (enumerator.Current.Required)
                    return true;
            }
            return false;
        }

        public void Reset()
        {
            enumerator.Reset();
        }

        public object Current
        {
            get { return enumerator.Current; }
        }
    }
}